import settings
from social.im.api import ZIMAPI
from social.utils.celery_app import app
from social.utils.jpush import JPush


@app.task()
def send_system_text_message(to_user_id, text):
    from social.account.models import User
    admin_user = User.objects.get(phone=130)
    ZIMAPI().send_text_message(from_user_id=admin_user.im_id, to_user_id=to_user_id, message=text)


@app.task()
def send_tip_message(from_user_id, to_user_id, text):
    ZIMAPI().send_custom_message(from_user_id, to_user_id, 3, text, sender_unaware=1)


@app.task()
def send_message_notification(from_user_id, to_user_id, content):
    from social.account.models import User
    from social.user.models import UserMsgSetting
    from_user = User.objects.get(id=from_user_id)
    to_user = User.objects.get(id=to_user_id)
    msg_setting, _ = UserMsgSetting.objects.get_or_create(user=to_user)
    if msg_setting.new_msg:
        content = content if msg_setting.msg_summary else '新消息'
        extra = {
            'title': settings.APP_TITLE,
            'content': content,
            'link': f'{settings.APP_NAME}://chat/{from_user.im_id}'
        }
        JPush.send_push(list([to_user.hid]), settings.APP_TITLE, content, content_type='chat',
                        extra=extra)
